package com.ruoyi.web.controller.agoniapp;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.JwtUtils;
import com.ruoyi.common.utils.TokenUser;
import com.ruoyi.common.utils.UserIdByRequest;
import com.ruoyi.system.pojo.dto.ListOtherProductsByPageDTO;
import com.ruoyi.system.pojo.entity.Product;
import com.ruoyi.system.pojo.vo.MyProductVO;
import com.ruoyi.system.pojo.vo.OtherProductVO;
import com.ruoyi.system.service.IAgoniProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/agoniapp/product")
public class ProductController {

    @Autowired
    private IAgoniProductService agoniProductService;
    @GetMapping("/listProducts")
    public AjaxResult listProducts(){
        return AjaxResult.success(agoniProductService.listProducts());
    }


    @GetMapping("/listServiceProducts/{storeId}")
    public AjaxResult listServiceProducts(@PathVariable Long storeId) {
        // 假设你有一个 service 方法来获取对应的产品
        List<Product> products = agoniProductService.listServiceProducts(storeId);
        return AjaxResult.success(products);
    }

    //添加产品
    @PostMapping("/addProduct")
    public AjaxResult addProduct(@RequestBody Product product ,HttpServletRequest request){
       Long userId = UserIdByRequest.getUserId(request);

       int result = agoniProductService.createProduct(product,userId);
       if (result == 1)
        return AjaxResult.success("添加成功");
       else
        return AjaxResult.error("添加失败");
    }

    //修改产品
    @PutMapping("/updateProduct/{id}")
    public AjaxResult updateProduct(@PathVariable Long id, @RequestBody Product product){
        log.info("updateProduct id:{}, product:{}", id, product);
        int result = agoniProductService.updateProduct(id, product);
        if(result == 1){
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }
    }


    /**
     * 根据token获取获取当前用户的产品列表
     */

    @GetMapping("/listServiceProductsByToken")
    public AjaxResult listServiceProductsByToken(HttpServletRequest request) {
        Long userId = UserIdByRequest.getUserId(request);
        // 假设你有一个 service 方法来获取对应的产品
        List<MyProductVO> myProductVOList = agoniProductService.listMyProductVO(userId);

        return AjaxResult.success("查询成功", myProductVOList);
    }


    /**
     * 获取所有商品列表
     *
     */
    @GetMapping("/listAllProducts")
    public   AjaxResult listAllProducts(HttpServletRequest request) {



        return AjaxResult.success("success");
    }
    //获取其他用户的产品列表
    @GetMapping("listOtherProducts")
    public AjaxResult listOtherProducts(HttpServletRequest request) {
        Long userId = UserIdByRequest.getUserId(request);

        List<OtherProductVO> otherProductVOList = agoniProductService.listOtherProducts(userId);


        return AjaxResult.success("查询成功", otherProductVOList);
    }


    //分页获取其他用户的产品列表
    @PostMapping("listOtherProductsByPage")
    public AjaxResult listOtherProductsByPage(HttpServletRequest request, @RequestBody ListOtherProductsByPageDTO listOtherProductsByPageDTO) {
        Long userId = UserIdByRequest.getUserId(request);

        PageInfo<OtherProductVO> otherProductVOList = agoniProductService.listOtherProductsByPage(userId,listOtherProductsByPageDTO);

        return AjaxResult.success("查询成功",otherProductVOList);
    }

    @PutMapping("changeProductStatus/{id}")
    public AjaxResult changeProductStatus(@PathVariable Long id) {

        int result = agoniProductService.changeProductStatus(id);
        if (result == 0) {
            return AjaxResult.error("修改失败");
            }

        return AjaxResult.success("修改成功");
    }


}
